package com.zychao.smex.db.service;

import java.util.Map;

import com.zychao.smex.db.dao.BookDao;
import com.zychao.smex.exception.CartNotFoundException;
import com.zychao.smex.po.Book;
import com.zychao.smex.po.Cart;
import com.zychao.smex.po.CartItem;

public class BusinessService {
	
	BookDao dao = new BookDao();
	
	public Map<String, Book> getAllBook() {
		return dao.getAllBook();
	}
	
	public void addToCart(String bookId, Cart cart) throws CartNotFoundException {
		if(cart == null) {
			throw new CartNotFoundException("Sorry, empty shopping cart!");
		}
		Book book = dao.findBook(bookId);
		cart.addToCart(book);
	}
	
	public void deleteBook(String bookId, Cart cart) throws CartNotFoundException {
		if(cart == null) {
			throw new CartNotFoundException("Sorry, empty shopping cart!");
		}
		cart.getMap().remove(bookId);
	}
	
	public void clearCart(Cart cart) throws CartNotFoundException {
		if(cart == null) {
			throw new CartNotFoundException("Sorry, empty shopping cart!");
		}
		cart.getMap().clear();
	}
	
	public void updateCart(String bookId, Cart cart, int count) throws CartNotFoundException {
		if(cart == null) {
			throw new CartNotFoundException("Sorry, empty shopping cart!");
		}
		CartItem item = cart.getMap().get(bookId);
		item.setCount(count);
	}
}
